package com.marinesky.mstools.compressor;

import com.marinesky.mstools.common.Constant;
import com.marinesky.mstools.file.FileUtil;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;

/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         佛祖保佑       永无BUG
   压缩CSS文件
*/
public class CssCompressor {

    //在指定位置换行(不换行)
    private static int lineBreak=-1;

    //压缩完后文件后缀
    private static String minSuffix=".min";

    //默认文件编码
    private static  String charset="utf-8";

    public static void setLineBreak(int lineBreak) {
        CssCompressor.lineBreak = lineBreak;
    }

    public static void setMinSuffix(String minSuffix) {
        CssCompressor.minSuffix = minSuffix;
    }

    public static void setCharset(String charset) {
        CssCompressor.charset = charset;
    }



    /**
     * 压缩CSS文件
     * @param in_file_path  -- 输入css文件
     * @param out_dir  -- 输出压缩后css文件目录
     * @return
     */
    public static boolean compressor(String in_file_path,String out_dir) {
        if(StringUtils.isBlank(in_file_path))
        {
            throw new IllegalArgumentException("the in_file_path is Blank");
        }
        File cssInFile=new File(in_file_path);
        return compressor(cssInFile,out_dir);
    }

    /**
     *  压缩CSS文件
     * @param in_file -- 输入css文件
     * @param out_dir -- 输出压缩后css文件目录
     * @return
     */
    public static boolean compressor(File in_file,String out_dir) {
        boolean result=false;
        String output_dir=out_dir;
        try{
            if(in_file.exists() && in_file.isFile())
            {
                //获取文件名(不包括后缀)
                String inFileName= FileUtil.getFileName(in_file);
                //获取文件的后缀(不包括点)
                String inFileExtension=FileUtil.getExtension(in_file);
                if(StringUtils.isBlank(output_dir)){
                    output_dir=in_file.getParentFile().getAbsolutePath();
                }
                //输出文件全路径
                String output_file=output_dir+File.separator+inFileName+minSuffix+ Constant.File.FILE_DOT+inFileExtension;

                if(StringUtils.isBlank(charset) || !Charset.isSupported(charset)){
                    throw new IllegalCharsetNameException("the charset is blank or not support!");
                }
                //读取源文件
                Reader in=new InputStreamReader(new FileInputStream(in_file),charset);

                //写压缩后文件
                Writer out=new OutputStreamWriter(new FileOutputStream(output_file),charset);

                com.yahoo.platform.yui.compressor.CssCompressor cssCompressor=new com.yahoo.platform.yui.compressor.CssCompressor(in);

                cssCompressor.compress(out,lineBreak);
                out.close();
                in.close();
                result=true;
            }
            else{
                throw new FileNotFoundException("the in_file not found !");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=false;
        }
        return result;
    }

    /**
     *压缩CSS文件,默认放在源文件目录
     * @param in_file_path --输入css文件路径
     * @return
     */
    public static boolean compressor(String in_file_path)
    {
        return compressor(in_file_path,null);
    }

    /**
     *压缩CSS文件,默认放在源文件目录
     * @param in_file --输入css文件
     * @return
     */
    public static boolean compressor(File in_file)
    {
        return compressor(in_file,null);
    }

}
